import json
import requests
from bs4 import BeautifulSoup

def get_thorchain_data():
    print("************跨链平台数据开始爬取************")
    # 存放跨链平台的数据
    list_data=[]
    for i in range(1,500):
        # 统计每一页有多少条数据合格
        count=0
        # 交易网站的网址
        url=f"https://viewblock.io/thorchain/txs?type=swap"
        headers = {
                'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
            }
        response = requests.get(url=url, headers=headers)
        print(response)
        data_html = response.text
        print(response.text)
        # 使用BeautifulSoup中的css选择器来提取数据
        soup=BeautifulSoup(data_html,'html.parser')
        data=soup.find(name='script',attrs={'type':'application/json'}).get_text()

        # 将json格式的数据转为python格式的数据
        dict_data=json.loads(data)

        # 继续过滤数据
        dict_data=dict_data.get("props").get("pageProps").get("ssrData").get(f"/thorchain/txs?page={i}\u0026type=streaming")
        # 筛选不带docs的关键字
        if dict_data is not None:
            list_data_tem=dict_data['docs']
            str_list=['input','output','outbounds','hash']
            #遍历数据列表，筛选带目标链交易哈希的
            for dic_tem_data in list_data_tem:
                # 筛选部分不带outbounds关键字的数据
                if "outbounds" in dic_tem_data:
                    if len(dic_tem_data["outbounds"])==2 and 'hash' in dic_tem_data["outbounds"][1]:
                        dic_filter={k:v for k,v in dic_tem_data.items() if k in str_list}
                        # 筛选源链和目标链不重复的数据
                        if dic_filter["input"]["chain"]!=dic_filter["output"]["chain"]:
                            list_data.append(dic_filter)
                            count=count+1
        print(f"第{i}页爬取完成，有{count}条数据合格！")

    # 将跨链平台抓取的数据返回
    print(f"thorchain平台数据爬取完成，总共{len(list_data)}条数据")
    json_data = json.dumps(list_data, indent=4)
    with open('../data_new/thorchain_data.json', 'w', encoding='utf-8') as f:
        f.write(json_data)
    return list_data;




